<?php

namespace app\xapply\middleware;

use app\xapply\model\Login;

/**
 * 登录验证 (适合大部分前端登录验证处理)
 */
class Auth
{
    public static function update($query){
        $object = Login::where('user_id', $query['user_id'])->find();
        if ($object){

        }else{
            $object = new Login;
        }
        $object->save($query);
    }

    public function handle($request, \Closure $next)
    {
        $get = getallheaders();
        if (isset($get['Authorization'])) {
            $auth = $get['Authorization'];
            $token = str_replace('Bearer ', '', $auth);
            if ($token){
                $user = Login::where('token', $token)->find();
                if ($user){
                    define('XAPPLY_UID', $user['user_id']);
                    return $next($request);
                }
            }
        }
        return json(['error' => '非法请求']);
    }
}
